<?php

namespace app\server;

use app\server\HredisServer;
use think\cache\driver\Redis;
use think\facade\Config;
use \think\facade\Db;
use think\facade\Request;
use think\facade\Cache;
use think\facade\Queue;

class KyyUserService
{
    public $table = 'app_kyy_user';
    public $event = [
        'sign'=>'签到领金币'
    ];

    public function getUser($params,$res = []){
        $row = find($this->table,'id,uuid,coins,total_coins,money,pass,ymd,create_at',['uuid'=>$params['uuid']]);
        if(empty($row)){
            $appArr = explode('_',$params['app_name']);
            $producer = $appArr[2]??'';
            $addData = [
                'uuid'=>$params['uuid'],
                'app_name'=>$params['app_name'],
                'producer'=>$producer,
                'path'=>$params['path']??'',
                'coins'=>0,
                'money'=>0,
                'ymd'=>date('Ymd',time())
            ];
            if(isset($res['res_code'])){
                $addData['res_code'] = $res['res_code'];
            }
            if(isset($res['pass'])){
                $addData['pass'] = $res['pass'];
            }
            $rtData = $addData;
            $rtData['create_at'] = $rtData['update_at'] = date('Y-m-d H:i:s');
            $uid = Db::table($this->table)->insertGetId($rtData);
            $rtData['id'] = $uid;
        }else{
            $rtData = $row;
        }
        return $rtData;
    }

    public function transCation($uid,$uuid,$coins,$type,$typeName,$sub = '+'){
        $trancationTable = 'app_kyy_user_transcation';
        $data = [
            'uid'=>$uid,
            'transcation_no'=>date('YmdHis',time()).mt_rand(1,9999999),
            'uuid'=>$uuid,
            'coins'=>$coins,
            'type'=>$type,
            'type_name'=>$typeName,
            'sub'=>$sub,
            'ymd'=>date('Ymd',time())
        ];
        add($trancationTable,$data);
        if($sub === '+'){
            Db::table($this->table)->where(['id'=>$uid])->inc('coins',$coins)->inc('total_coins',$coins)->update();
        }elseif($sub === '-'){
            Db::table($this->table)->where(['id'=>$uid])->dec('coins',$coins)->update();
        }
        return $data;
    }

}


?>
